package com.nix.web.controller.word;

import com.nix.constant.Consts;
import com.nix.web.databases.pojo.user.User;
import com.nix.web.databases.pojo.word.MarkWord;
import com.nix.web.databases.pojo.word.emtion.Emtion;
import com.nix.web.databases.pojo.word.emtion.NounEmtion;
import com.nix.web.databases.service.word.MarkWordService;
import com.nix.web.databases.service.word.emotion.EmotionService;
import com.nix.web.databases.service.word.emotion.NounEmtionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;

/**
 * Title: Emotional-Analysis
 * Date: Create in 2018/1/26 19:17
 * Description:
 *
 * @author Jundger
 * @version 1.0
 */
@RequestMapping("/word")
@Controller
public class WordController {

    @Autowired
    private EmotionService emotionService;

    @Autowired
    private NounEmtionService nounEmtionService;

    @Autowired
    private MarkWordService markWordService;

    @RequestMapping(value = "/save", method = {RequestMethod.GET, RequestMethod.POST})
    public String saveMarkWords(HttpServletRequest request, HttpServletResponse response, Model model, HttpSession session) {
        System.out.println("comming.....");
        String[] word_id = request.getParameterValues("word_id");
        String[] polarity = request.getParameterValues("polarity");
        String[] strength = request.getParameterValues("strength");
        String[] tension = request.getParameterValues("tension");
        String[] direction = request.getParameterValues("directionality");
        ArrayList<Emtion> emtions = new ArrayList<Emtion>();


        User user = (User) session.getAttribute("user");
//        System.out.println("save Mark user -->" + user.toString());

        try {
            ArrayList<Emtion> list = emotionService.getAllEmotion();
            System.out.println("MarkWord length -->" + list.size());

            for(int i = 0; i < direction.length; ++i) {
                Emtion emtion = new Emtion();
                emtion.setDirectionality(direction[i]);
                emtion.setPolarity(polarity[i]);
                emtion.setStrength(strength[i]);
                emtion.setTension(tension[i]);
                emtions.add(emtion);

                Integer index = indexOfEmotion(list, emtion);

                // session缓存里存在相同情绪
                if (index != -1) {
                    System.out.println("Have emotion(" + word_id[i] + ") -->" + emtion.toString());
                    int emotion_id = nounEmtionService.getEmotionId(index);
                    System.out.println("emotion_id -->" + emotion_id);
                    nounEmtionService.saveMarkResult(Integer.parseInt(word_id[i]), emotion_id, user.getId());
                    System.out.println("Mark Word Save Success!");

                } else if (!"0".equals(polarity[i]) && !"0".equals(strength[i]) && !"0".equals(tension[i]) && !"0".equals(direction[i])){

                    System.out.println("Don't have emotion(" + word_id[i] + ") -->" + emtion.toString());

                    int id1 = nounEmtionService.addEmotion(emtion);
                    System.out.println("word_emtion id: " + emtion.getId());
                    list.add(emtion); // 修改数据库的同时修改缓存中的数据

                    NounEmtion nounEmtion = new NounEmtion();
                    nounEmtion.setEmtion_id(emtion.getId());
                    int id2 = nounEmtionService.addNounEmotion(nounEmtion);
                    System.out.println("word_noun_emtion: " + nounEmtion.getNoun_emtion_id());

                    nounEmtionService.saveMarkResult(Integer.parseInt(word_id[i]), nounEmtion.getNoun_emtion_id(), user.getId());
                    System.out.println("Mark Word Save Success!");
                }
            }

            Integer markCount;
            Integer microBlogCount;
            MarkWord[] words;

            markCount = markWordService.getMarkCountById(user.getId());
            microBlogCount = markWordService.getMarkWeiboCountById(user.getId());
            words = markWordService.getMarkWord(user.getId(), Consts.WORD_MARK_REPETITIONS, Consts.WORD_MARK_SEARCH_COUNT, "名词");
            model.addAttribute("words", words);
            model.addAttribute("markCount", markCount);
            model.addAttribute("microBlogCount", microBlogCount);
        } catch (Exception e) {
            e.printStackTrace();
        }

//        request.getRequestDispatcher("/app/word/mark").forward(request, response);
//        response.sendRedirect("/app/word/mark.jsp");

        return "forward:/view/app/word/mark.jsp";
    }

    Integer indexOfEmotion(ArrayList<Emtion> list, Emtion emtion) {
        Integer index = -1;

        for (Emtion e :
                list) {

            if (e.getPolarity().equals(emtion.getPolarity())
                    && e.getStrength().equals(emtion.getStrength())
                    && e.getTension().equals(emtion.getTension())
                    && e.getDirectionality().equals(emtion.getDirectionality())) {
                index = e.getId();
            }
        }

        return index;
    }
}
